package com.coldchain.admin.user.service.impl;

import com.coldchain.admin.user.entity.Company;
import com.coldchain.admin.user.entity.CompanyAccount;
import com.coldchain.admin.user.entity.CompanyAmountUseLogs;
import com.coldchain.admin.user.mapper.CompanyAccountMapper;
import com.coldchain.admin.user.mapper.CompanyMapper;
import com.coldchain.admin.user.service.ICompanyAmountUseLogsService;
import com.coldchain.admin.user.service.ICompanyService;
import com.coldchain.common.enums.AccountStatusEnum;
import com.coldchain.common.exception.base.BusinessException;
import com.coldchain.common.util.MD5Utils;
import com.coldchain.common.util.Pinyin4j;
import com.fangliai.core.base.service.impl.BaseServiceImpl;
import com.fangliai.core.context.BaseContextHandler;
import org.apache.ibatis.builder.BuilderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 企业信息表 服务实现类
 * </p>
 *
 * @author fangliai
 * @since 2018-09-20
 */
@Service
public class CompanyServiceImpl extends BaseServiceImpl<CompanyMapper, Company> implements ICompanyService {
    private static Logger log= LoggerFactory.getLogger(CompanyServiceImpl.class);
    @Autowired
    private ICompanyAmountUseLogsService amountUseLogsService;

    @Autowired
    private CompanyAccountMapper accountMapper;

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void useAmountPayOrder(String comId,String orderNo,BigDecimal amount) throws  BusinessException{
         Company company=getById(comId);
         if(company==null){
             throw new BusinessException("找不到该企业");
         }
         if(company.getAmount().subtract(company.getUseAmount()).compareTo(amount)<0){
             throw  new BusinessException("额度不足");
         }
         try {
             company.setUseAmount(company.getUseAmount().add(amount));
             updateById(company);
             CompanyAmountUseLogs logs=new CompanyAmountUseLogs();
             logs.setAmount(amount);
             logs.setOrderNo(orderNo);
             logs.setCompayId(comId);
             logs.setUseTime(new Date().getTime());
             logs.setYear(Calendar.getInstance().get(Calendar.YEAR));
             logs.setMonth(Calendar.getInstance().get(Calendar.MONTH));
             amountUseLogsService.save(logs);
         }catch (Exception e){
              log.error("使用信用额支付订单失败:error"+e.getMessage());
             throw new BusinessException("支付失败");
         }
    }

    @Override
    public Map<String, Object> getCompanyInfo(String id) {
        return baseMapper.getCompanyInfo(id);
    }

    @Override
    @Transactional
    public void register(Company company)throws Exception {
        try{
            company.setPinyin(new Pinyin4j().toPinYinUppercaseInitials(company.getCompanyName()));
            company.setCreateBy(BaseContextHandler.getUsername());
            company.setCreateTime(new Date().getTime());
            super.save(company);
            CompanyAccount account=new CompanyAccount();
            account.setPassword(MD5Utils.getDefalutPassword());
            account.setAccountName("admin");
            account.setAccountPhone(company.getPhone());
            account.setCompanyId(company.getCompanyId());
            account.setEmail(company.getEmail());
            account.setRoles("admin");
            account.setStatus(AccountStatusEnum.ENABLE);
            accountMapper.insert(account);
        }catch (Exception e){
             log.error("注册企业失败,error:"+e.getMessage());
             throw  new BuilderException("error");
        }

    }

}
